package com.sls.web.manager.action.system;

import java.util.List;

import javax.annotation.Resource;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.sys.pojo.Permission;
import com.sls.web.manager.sys.pojo.RolePermission;
import com.sls.web.manager.sys.service.MenuBuildService;
import com.sls.web.manager.sys.service.RolePermissionService;

public class RolePermissionAction extends SuperAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3068943440740517983L;
	@Resource(name = "rolePermissionService")
	private RolePermissionService rolePermissionService;
	@Resource(name = "menuBuildService")
	private MenuBuildService menuBuildService;
	private Pagination<RolePermission> page;
	private RolePermission rp;
	private String roleId;
	private List<Permission> ps;
	private List<Permission> notPs;
	private List<String> codes;

	public String list() throws Exception {

		if (rp == null) {
			rp = new RolePermission();
			rp.setCurrentPage(Constants.INIT_CURRENTPAGE);
			rp.setPageSize(Constants.INIT_PAGESIZE);
		}

		page = rolePermissionService.querRolepermission(rp,
				getCurrentOperator());

		return "list";
	}

	public String edit() throws Exception {
		List<Permission> list = rolePermissionService
				.queryRolepermissionByRoleId(roleId, getCurrentOperator());
		ps = menuBuildService.getAllMenu();
		notPs=menuBuildService.getAllNOtMenu();
		for (Permission p : ps) {
			for (Permission p1 : list) {
				if (p.getCode().equals(p1.getCode())) {
					p.setOpt(true);
					break;
				} else {
					p.setOpt(false);
				}
			}
		}
		return "edit";
	}

	public String update() throws Exception {
		rolePermissionService.updateRPByRoleId(codes, roleId,
				getCurrentOperator());
		return list();
	}

	public Pagination<RolePermission> getPage() {
		return page;
	}

	public void setPage(Pagination<RolePermission> page) {
		this.page = page;
	}

	public RolePermission getRp() {
		return rp;
	}

	public void setRp(RolePermission rp) {
		this.rp = rp;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public List<Permission> getPs() {
		return ps;
	}

	public void setPs(List<Permission> ps) {
		this.ps = ps;
	}

	public List<String> getCodes() {
		return codes;
	}

	public void setCodes(List<String> codes) {
		this.codes = codes;
	}

	public List<Permission> getNotPs() {
		return notPs;
	}

	public void setNotPs(List<Permission> notPs) {
		this.notPs = notPs;
	}

}
